package org.sjs.dreamfly.biz.movie

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import kotlinx.coroutines.launch
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.data.Config

class XiGuaMovieListFragment: MovieListFragment() {

    companion object {
        fun newInstance(tab: String): Fragment {
            val fragment = XiGuaMovieListFragment()
            fragment.arguments = Bundle().apply {
                putString(Router.KEY_CHANNEL_TYPE, tab)
            }
            return fragment
        }
    }

    override var channelType: String = Config.defaultChannelType4XiGua

    override fun onCreate(savedInstanceState: Bundle?) {
        arguments?.getString(Router.KEY_CHANNEL_TYPE)?.let {
            channelType = it
        }
        super.onCreate(savedInstanceState)
    }

    override fun onInitViews() {
        super.onInitViews()
        useWebViewMask(true)
        showLoadingView()
        useMacChromeUserAgent()
        webViewClientAcceptVisitor(object : WebViewClient() {
            override fun onLoadResource(view: WebView?, url: String?) {
                if (MovieUtil.isLoadMoreApi(url?:"")) {
                    view?.let {
                        viewModel.onWebLoadMore(view, url!!)
                    }
                }
                super.onLoadResource(view, url)
            }
        })
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onLoadData() {
        mainScope.launch {
            viewModel.loadMovieList()
            hideLoadingView()
            recyclerView.adapter?.notifyDataSetChanged()
        }
        loadUrl(MovieConstants.getXiGuaChannelFilterPageUrl(channelType))
    }
}